﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmPaperPaceManager : FrmMDIChildFormTemplate
    {
        PaperFaceController         _controller = new PaperFaceController();
        paperFace                   _paperFace = null;
        static FrmPaperPaceManager instance = null;
        private FrmPaperPaceManager()
        {
            InitializeComponent();
        }
        public static FrmPaperPaceManager CreateInstance
        {
            get
            {
                if (instance == null)
                    instance = new FrmPaperPaceManager();
                return instance;
            }
        }
        private void FrmPaperPaceManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlPaperFace;
            _controller.View = gridViewPaperFace;
            _controller.GetDate();
        }

        // 添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtPaperFace.Focus();
                return;
            }
            if (CheckData())
            {
                paperFace obj = new paperFace();
 
                obj.Name = txtPaperFace.Text;
                obj.remark = txtPaperFaceRemark.Text;
                obj.Status = 1;
                _controller.Add(obj);

                //link datagrid
                _controller.Grid = gridControlPaperFace;
                _controller.View = gridViewPaperFace;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        // 更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtPaperFace.Focus();
                return;
            }
            if (CheckData())
            {
                _paperFace.Name = txtPaperFace.Text;
                _paperFace.remark = txtPaperFaceRemark.Text;
                _controller.Update(_paperFace);

                //link datagrid
                _controller.Grid = gridControlPaperFace;
                _controller.View = gridViewPaperFace;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        // 启用
        private void btnUseable_Click(object sender, EventArgs e)
        {
            _controller.Useable(_paperFace);

            //link datagrid
            _controller.Grid = gridControlPaperFace;
            _controller.View = gridViewPaperFace;
            _controller.GetDate();
        }
        // 停用
        private void btndisable_Click(object sender, EventArgs e)
        {
            if (_paperFace.Name != "任意")
            {
                if (DialogResult.OK == MessageBox.Show("确认停用吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    _controller.Disable(_paperFace);

                    //link datagrid
                    _controller.Grid = gridControlPaperFace;
                    _controller.View = gridViewPaperFace;
                    _controller.GetDate();

                }
            }
            else
            {
                MessageBox.Show("系统数据不能停用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }
        // 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_paperFace.Name != "任意")
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {

                    _controller.Delete(_paperFace);

                    //link datagrid
                    _controller.Grid = gridControlPaperFace;
                    _controller.View = gridViewPaperFace;
                    _controller.GetDate();
                }
            }
            else
            {
                MessageBox.Show("系统数据不能删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtPaperFaceRemark.Enabled = false;
            txtPaperFace.Enabled = false;


            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnUpdate.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;
            btndisable.Enabled = true;
            btnUseable.Enabled = true;
        }
        private void gridViewPaperFace_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperFace = (paperFace)gridViewPaperFace.GetFocusedRow();
            if (_paperFace != null)
            {
                txtPaperFace.Text = _paperFace.Name;
                txtPaperFaceRemark.Text = _paperFace.remark;
            }
        }
        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtPaperFace.Text == string.Empty)
            {
                txtPaperFace.Focus();
                MessageBox.Show("纸面名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtPaperFaceRemark.Text == string.Empty)
            {
                txtPaperFaceRemark.Focus();
                MessageBox.Show("纸面标志不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtPaperFace.Enabled = bl;
            txtPaperFaceRemark.Enabled = bl;

            txtPaperFace.Text = string.Empty;
            txtPaperFaceRemark.Text = string.Empty;

            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btndisable.Enabled = false;
                btnUseable.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
                btndisable.Enabled = true;
                btnUseable.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {

            txtPaperFace.Enabled = bl;
            txtPaperFaceRemark.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
                btndisable.Enabled = false;
                btnUseable.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
                btndisable.Enabled = true;
                btnUseable.Enabled = true;
            }
        }

        private void FrmPaperPaceManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }
    }
}
